import java.util.HashMap;
import java.util.Map;

public class Leetcode1582 {
    public static void main(String[] args) {
        System.out.println(numSpecial(new int[][]{
                {0, 0, 1, 0},
                {0, 0, 0, 0},
                {0, 0, 0, 0},
                {0, 1, 0, 0}
        }));

    }

    public static int numSpecial(int[][] mat) {
        int res = 0;
        Map<Integer, Integer> map = new HashMap<>();

        loop:
        for (int rowIndex = 0; rowIndex < mat.length; rowIndex++) {
            int index = 0;
            boolean flag = false;
            for (int cellIndex = 0; cellIndex < mat[rowIndex].length; cellIndex++) {
                if (mat[rowIndex][cellIndex] == 1) {
                    if (flag) {
                        continue loop;
                    }

                    flag = true;
                    index = cellIndex;
                }
            }

            if (flag)
                map.put(rowIndex, index);
        }

        loop:
        for (int cellIndex = 0; cellIndex < mat[0].length; cellIndex++) {
            int index = 0;
            boolean flag = false;
            for (int rowIndex = 0; rowIndex < mat.length; rowIndex++) {
                if (mat[rowIndex][cellIndex] == 1) {
                    if (flag) {
                        continue loop;
                    }

                    flag = true;
                    index = rowIndex;
                }
            }

            if (flag) {
                if (map.containsKey(index)) {
                    res++;
                }
            }

        }

        return res;
    }


}
